package com.jgzx.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jgzx.common.R;
import com.jgzx.domain.IdDTO;
import com.jgzx.domain.ListVO;
import com.jgzx.domain.dto.EnterprisePublicityDTO;
import com.jgzx.domain.po.EnterprisePublicityPO;
import com.jgzx.domain.vo.EnterprisePublicityVO;
import com.jgzx.mapper.EnterprisePublicityMapper;
import com.jgzx.utils.AppContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 企业宣传视频Service
 * @author libo
 * @date 2021/10/22 16:34
 */
@Slf4j
@Service
public class EnterprisePublicityService extends ServiceImpl<EnterprisePublicityMapper, EnterprisePublicityPO> {


    /**
     * 查询当前企业的宣传视频列表
     */
    public R<ListVO<EnterprisePublicityVO>> listAll() {
        List<EnterprisePublicityVO> records = this.baseMapper.listAll(AppContextUtil.getEnterpriseId());
        ListVO<EnterprisePublicityVO> enterprisePublicityVOList = new ListVO<>(records);
        log.info("查询企业宣传视频列表:records={}", enterprisePublicityVOList);
        return R.ok(enterprisePublicityVOList);
    }

    /**
     * 获取宣传视频详情
     *
     * @param idDTO 宣传视频主键
     */
    public R<EnterprisePublicityVO> getPublicity(IdDTO idDTO) {
        EnterprisePublicityVO enterprisePublicityVO = this.baseMapper.getPublicity(idDTO, AppContextUtil.getEnterpriseId());
        log.info("查询企业宣传视频详情={}", enterprisePublicityVO);
        return R.ok(enterprisePublicityVO);
    }

    /**
     * 新增宣传视频
     *
     * @param enterprisePublicityDTO 宣传视频DTO
     */
    @Transactional
    public R<EnterprisePublicityVO> addPublicity(EnterprisePublicityDTO enterprisePublicityDTO) {
        if (ObjectUtil.isNotNull(enterprisePublicityDTO.getEnterprise_publicity_id())) {
            return R.fail("新增宣传视频主键不能有值");
        }
        EnterprisePublicityPO enterprisePublicityPO = BeanUtil.copyProperties(enterprisePublicityDTO, EnterprisePublicityPO.class);
        enterprisePublicityPO.setEnterprise_id(AppContextUtil.getEnterpriseId());
        this.save(enterprisePublicityPO);
        return getPublicity(new IdDTO(enterprisePublicityPO.getEnterprise_publicity_id()));
    }

    /**
     * 更新宣传视频
     *
     * @param enterprisePublicityDTO 宣传视频STO
     */
    @Transactional
    public R<EnterprisePublicityVO> updatePublicity(EnterprisePublicityDTO enterprisePublicityDTO) {
        if (ObjectUtil.isNull(enterprisePublicityDTO.getEnterprise_publicity_id())) {
            return R.fail("更新宣传视频，主键必须有值");
        }
        EnterprisePublicityPO alreadyEnterprisePublicityPO = lambdaQuery().eq(EnterprisePublicityPO::getEnterprise_publicity_id, enterprisePublicityDTO.getEnterprise_publicity_id())
                .eq(EnterprisePublicityPO::getEnterprise_id, AppContextUtil.getEnterpriseId())
                .one();
        if (ObjectUtil.isNull(alreadyEnterprisePublicityPO)) {
            return R.fail("数据被他人修改，请刷新后重试");
        }
        EnterprisePublicityPO enterprisePublicityPO = BeanUtil.copyProperties(enterprisePublicityDTO, EnterprisePublicityPO.class);
        this.updateById(enterprisePublicityPO);
        return getPublicity(new IdDTO(enterprisePublicityPO.getEnterprise_publicity_id()));
    }

    /**
     * 删除宣传视频
     *
     * @param idDTO 宣传视频主键
     */
    @Transactional
    public R delPublicity(IdDTO idDTO) {
        EnterprisePublicityPO alreadyEnterprisePublicityPO = lambdaQuery().eq(EnterprisePublicityPO::getEnterprise_publicity_id, idDTO.getId())
                .eq(EnterprisePublicityPO::getEnterprise_id, AppContextUtil.getEnterpriseId())
                .one();
        if (ObjectUtil.isNull(alreadyEnterprisePublicityPO)) {
            return R.fail("数据被他人修改，请刷新后重试");
        }
        this.removeById(idDTO.getId());
        return R.ok("删除成功");
    }
}
